package com.red.wood.module.eval.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.springjdbc.CommonDao;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;
import com.red.wood.module.eval.dao.EvalProjectDao;

@Repository
public class EvalProjectDaoImpl implements EvalProjectDao{
	
	@Autowired
	private CommonDao dao;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public void add(Map<String, Object> paramMap) {
		StringBuilder sqlSb = new StringBuilder();
		sqlSb.append("insert into t_eval_project (");
		dao.insertTableData(paramMap, sqlSb);
	}

	@Override
	public void update(IMap paramMap) {
		String singleTaskId = CommonUtils.valueOf(paramMap.get("singleTaskId"));
		String type = CommonUtils.valueOf(paramMap.get("type"));
		String realIndex = CommonUtils.valueOf(paramMap.get("realIndex"));
		if(StringUtils.isNotBlank(singleTaskId) && StringUtils.isNotBlank(realIndex) && StringUtils.isNotBlank(type)){
			StringBuilder sqlSb = new StringBuilder();
			sqlSb.append("update t_eval_project set ");
			String queryConf = " where singleTaskId=" + singleTaskId +" and realIndex=" + realIndex + " and type=" + "'" + type + "'";
			dao.updateTableData(paramMap, sqlSb, queryConf);
		}
	}

	@Override
	public List<Map<String, Object>> getData(String singleTaskId) {
		if(StringUtils.isNotBlank(singleTaskId)){
			String sql = "select id as id, singleTaskId as singleTaskId, evalNum as evalNum, result as result, type as type"
					+ ", parentProject as parentProject,  project as project, reason as reason, realIndex as realIndex "
					+ "from t_eval_project where singleTaskId =" + singleTaskId;
			return jdbcTemplate.queryForList(sql);
		}
		return null;
	}

	@Override
	public int getCountBySingleTaskId(String singleTaskId) {
		if(StringUtils.isNotBlank(singleTaskId)){
			String sql = "select count(1) as count from t_eval_project where singleTaskId = " + singleTaskId;
			List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql);
			if(dataList != null && !dataList.isEmpty()){
				return Integer.valueOf(dataList.get(0).get("count").toString());
			}
		}
		return 0;
	}
	
	public int getBySingleTaskIdAndIndex(String singleTaskId, String realIndex, String type){
		if(StringUtils.isNotBlank(singleTaskId)){
			String sql = "select count(1) as count from t_eval_project where singleTaskId = " + singleTaskId
			 +" and realIndex =" + realIndex
			 + " and type = " + "'" + type + "'";
			List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql);
			if(dataList != null && !dataList.isEmpty()){
				return Integer.valueOf(dataList.get(0).get("count").toString());
			}
		}
		return 0;
	}
	
	@Override
	public List<Map<String, Object>> getBySingleTaskIdAndResult(String singleTaskId, String result) {
		if(StringUtils.isNotBlank(singleTaskId)){
			String sql = "select id as id, singleTaskId as singleTaskId, evalNum as evalNum, result as result, type as type"
					+ ", parentProject as parentProject,  project as project, reason as reason, realIndex as realIndex "
					+ "from t_eval_project where singleTaskId =" + singleTaskId
					+ " and result =" + result;
			return jdbcTemplate.queryForList(sql);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getEvalProjectResult(String singleTaskId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(1) count");
		sql.append("  from t_eval_dictionary dict");
		sql.append(" inner join (select t1.type, sum(t2.result) result");
		sql.append("               from t_eval_singletask t1");
		sql.append("              inner join t_eval_project t2");
		sql.append("                 on t1.id = t2.singleTaskId");
		sql.append("              where t2.result = 1");
		sql.append("              and t1.id = "+singleTaskId);
		sql.append("              group by t1.type) t");
		sql.append("    on dict.CODE_KEY = t.type");
		sql.append("   and dict.CODE_VALUE = t.result");
		return jdbcTemplate.queryForList(sql.toString());
	}
}
